package model;

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import view.AddWindow;
import view.ModifyWindow;

/**
 * Save the modifications of the data for a contact in the text file
 * @author Marie Morin
 *
 */

public class SaveModificationsModel {
	
	private ModifyWindow m;
	private Contact c, contact;
	private ContactListModel contactL = new ContactListModel();
	private ArrayList<Contact> contactsList;
	
	public SaveModificationsModel(ModifyWindow m, Contact contact){
		
		this.m = m;
		
		this.contactsList = contactL.getContactsList();
		
		String aname=" ";
		String name= " ";
		String fname= " ";
		String phone= " ";
		String professionalPhone=" ";
		String homeAddress=" ";
		String professionalAddress=" ";
		String email="-";
		String group=" ";
		String website="-";
		
		if (!m.getJtf_name().getText().equals(""))
			name = m.getJtf_name().getText();
		
		if (!m.getJtf_fname().getText().equals(""))
			fname=m.getJtf_fname().getText();
		
			
		if (!m.getJtf_phone().getText().equals(""))
			phone =m.getJtf_phone().getText();
		
		if (!m.getJtf_professionalPhone().getText().equals(""))
			professionalPhone = m.getJtf_professionalPhone().getText();
		
		if (!m.getJtf_homeAddress().getText().equals(""))
			homeAddress = m.getJtf_homeAddress().getText();
		
		if (!m.getJtf_professionalAddress().getText().equals(""))
			professionalAddress = m.getJtf_professionalAddress().getText();
		
		if (!m.getJtf_email().getText().equals(""))
			email = m.getJtf_email().getText();
			
		if (!m.getJtf_group().getText().equals(""))
			group = m.getJtf_group().getText();
		
		if (!m.getJtf_website().getText().equals(""))
			website = m.getJtf_website().getText();
		
		aname = name +" "+ fname;
		
		this.c = new Contact(name, fname, aname, phone, professionalPhone, homeAddress, professionalAddress, email, group, website, contact.getDate());
		
		
		if (contactsList.size()>0){
			
			for(int i=0; i<contactsList.size(); i++){
				
		        if (equal(contactsList.get(i), contact)){
		       

		        	contactsList.get(i).setAname(c.getAname());
		        	contactsList.get(i).setName(c.getName());
		        	contactsList.get(i).setFname(c.getFname());
		        	contactsList.get(i).setPhone(c.getPhone());
		        	contactsList.get(i).setEmail(c.getEmail());
		        	contactsList.get(i).setWebsite(c.getWebsite());
		        	contactsList.get(i).setHomeAddress(c.getHomeAddress());
		        	contactsList.get(i).setProfessionalPhone(c.getProfessionalPhone());
		        	contactsList.get(i).setProfessionalAddress(c.getProfessionalAddress());
		        	contactsList.get(i).setGroup(c.getGroup());
		       
		        }
		    }
			
		}
		
		
		try {
			this.modifyFile();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	
	
	public boolean equal(Contact a, Contact b){
		
		boolean test = false;
		
		if (a.getAname().equals(b.getAname())){
			test = true;
		}
		
		return test;
	}
	
	/**
	 * Save the modifications of the data for a contact in the text file
	 */
		public void modifyFile() throws FileNotFoundException, UnsupportedEncodingException{
		
		PrintWriter writer = new PrintWriter("test.txt", "UTF-8");
		
		if (contactsList.size()>0){
			

			for(int i=0; i<contactsList.size()-1; i++){
			
				writer.println(contactsList.get(i).getName());
				writer.println(contactsList.get(i).getFname());
				writer.println(contactsList.get(i).getPhone());
				writer.println(contactsList.get(i).getEmail());
				writer.println(contactsList.get(i).getWebsite());
				writer.println(contactsList.get(i).getHomeAddress());
				writer.println(contactsList.get(i).getProfessionalPhone());
				writer.println(contactsList.get(i).getProfessionalAddress());
				writer.println(contactsList.get(i).getGroup());
				writer.println(contactsList.get(i).getDate());
				
			}
			
			writer.println(contactsList.get(contactsList.size()-1).getName());
			writer.println(contactsList.get(contactsList.size()-1).getFname());
			writer.println(contactsList.get(contactsList.size()-1).getPhone());
			writer.println(contactsList.get(contactsList.size()-1).getEmail());
			writer.println(contactsList.get(contactsList.size()-1).getWebsite());
			writer.println(contactsList.get(contactsList.size()-1).getHomeAddress());
			writer.println(contactsList.get(contactsList.size()-1).getProfessionalPhone());
			writer.println(contactsList.get(contactsList.size()-1).getProfessionalAddress());
			writer.println(contactsList.get(contactsList.size()-1).getGroup());
			writer.print(contactsList.get(contactsList.size()-1).getDate());

			writer.close();	
			
		}
				
		
	}
	

}
